home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Net / Cmd.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  11.0 KB  |  540 lines

  1.  
  2. package Net::Cmd;
  3.  
  4. require 5.001;
  5. require Exporter;
  6.  
  7. use strict;
  8. use vars qw(@ISA @EXPORT $VERSION);
  9. use Carp;
  10.  
  11. $VERSION = "2.0801";
  12. @ISA     = qw(Exporter);
  13. @EXPORT  = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
  14.  
  15. sub CMD_INFO    { 1 }
  16. sub CMD_OK    { 2 }
  17. sub CMD_MORE    { 3 }
  18. sub CMD_REJECT    { 4 }
  19. sub CMD_ERROR    { 5 }
  20. sub CMD_PENDING { 0 }
  21.  
  22. my %debug = ();
  23.  
  24. sub _print_isa
  25. {
  26.  no strict qw(refs);
  27.  
  28.  my $pkg = shift;
  29.  my $cmd = $pkg;
  30.  
  31.  $debug{$pkg} ||= 0;
  32.  
  33.  my %done = ();
  34.  my @do   = ($pkg);
  35.  my %spc = ( $pkg , "");
  36.  
  37.  print STDERR "\n";
  38.  while ($pkg = shift @do)
  39.   {
  40.    next if defined $done{$pkg};
  41.  
  42.    $done{$pkg} = 1;
  43.  
  44.    my $v = defined ${"${pkg}::VERSION"}
  45.                 ? "(" . ${"${pkg}::VERSION"} . ")"
  46.                 : "";
  47.  
  48.    my $spc = $spc{$pkg};
  49.    print STDERR "$cmd: ${spc}${pkg}${v}\n";
  50.  
  51.    if(defined @{"${pkg}::ISA"})
  52.     {
  53.      @spc{@{"${pkg}::ISA"}} = ("  " . $spc{$pkg}) x @{"${pkg}::ISA"};
  54.      unshift(@do, @{"${pkg}::ISA"});
  55.     }
  56.   }
  57.  
  58.  print STDERR "\n";
  59. }
  60.  
  61. sub debug
  62. {
  63.  @_ == 1 or @_ == 2 or croak 'usage: $obj->debug([LEVEL])';
  64.  
  65.  my($cmd,$level) = @_;
  66.  my $pkg = ref($cmd) || $cmd;
  67.  my $oldval = 0;
  68.  
  69.  if(ref($cmd))
  70.   {
  71.    $oldval = ${*$cmd}{'net_cmd_debug'} || 0;
  72.   }
  73.  else
  74.   {
  75.    $oldval = $debug{$pkg} || 0;
  76.   }
  77.  
  78.  return $oldval
  79.     unless @_ == 2;
  80.  
  81.  $level = $debug{$pkg} || 0
  82.     unless defined $level;
  83.  
  84.  _print_isa($pkg)
  85.     if($level && !exists $debug{$pkg});
  86.  
  87.  if(ref($cmd))
  88.   {
  89.    ${*$cmd}{'net_cmd_debug'} = $level;
  90.   }
  91.  else
  92.   {
  93.    $debug{$pkg} = $level;
  94.   }
  95.  
  96.  $oldval;
  97. }
  98.  
  99. sub message
  100. {
  101.  @_ == 1 or croak 'usage: $obj->message()';
  102.  
  103.  my $cmd = shift;
  104.  
  105.  wantarray ? @{${*$cmd}{'net_cmd_resp'}}
  106.            : join("", @{${*$cmd}{'net_cmd_resp'}});
  107. }
  108.  
  109. sub debug_text { $_[2] }
  110.  
  111. sub debug_print
  112. {
  113.  my($cmd,$out,$text) = @_;
  114.  print STDERR $cmd,($out ? '>>> ' : '<<< '), $cmd->debug_text($out,$text);
  115. }
  116.  
  117. sub code
  118. {
  119.  @_ == 1 or croak 'usage: $obj->code()';
  120.  
  121.  my $cmd = shift;
  122.  
  123.  ${*$cmd}{'net_cmd_code'} = "000"
  124.     unless exists ${*$cmd}{'net_cmd_code'};
  125.  
  126.  ${*$cmd}{'net_cmd_code'};
  127. }
  128.  
  129. sub status
  130. {
  131.  @_ == 1 or croak 'usage: $obj->status()';
  132.  
  133.  my $cmd = shift;
  134.  
  135.  substr(${*$cmd}{'net_cmd_code'},0,1);
  136. }
  137.  
  138. sub set_status
  139. {
  140.  @_ == 3 or croak 'usage: $obj->set_status(CODE, MESSAGE)';
  141.  
  142.  my $cmd = shift;
  143.  my($code,$resp) = @_;
  144.  
  145.  $resp = [ $resp ]
  146.     unless ref($resp);
  147.  
  148.  (${*$cmd}{'net_cmd_code'},${*$cmd}{'net_cmd_resp'}) = ($code, $resp);
  149.  
  150.  1;
  151. }
  152.  
  153. sub command
  154. {
  155.  my $cmd = shift;
  156.  
  157.  $cmd->dataend()
  158.     if(exists ${*$cmd}{'net_cmd_lastch'});
  159.  
  160.  if (scalar(@_))
  161.   {
  162.    my $str =  join(" ",@_) . "\015\012";
  163.  
  164.    syswrite($cmd,$str,length $str);
  165.  
  166.    $cmd->debug_print(1,$str)
  167.     if($cmd->debug);
  168.  
  169.    ${*$cmd}{'net_cmd_resp'} = [];      # the response
  170.    ${*$cmd}{'net_cmd_code'} = "000";    # Made this one up :-)
  171.   }
  172.  
  173.  $cmd;
  174. }
  175.  
  176. sub ok
  177. {
  178.  @_ == 1 or croak 'usage: $obj->ok()';
  179.  
  180.  my $code = $_[0]->code;
  181.  0 < $code && $code < 400;
  182. }
  183.  
  184. sub unsupported
  185. {
  186.  my $cmd = shift;
  187.  
  188.  ${*$cmd}{'net_cmd_resp'} = [ 'Unsupported command' ];
  189.  ${*$cmd}{'net_cmd_code'} = 580;
  190.  0;
  191. }
  192.  
  193. sub getline
  194. {
  195.  my $cmd = shift;
  196.  
  197.  ${*$cmd}{'net_cmd_lines'} ||= [];
  198.  
  199.  return shift @{${*$cmd}{'net_cmd_lines'}}
  200.     if scalar(@{${*$cmd}{'net_cmd_lines'}});
  201.  
  202.  my $partial = ${*$cmd}{'net_cmd_partial'} || "";
  203.  
  204.  my $rin = "";
  205.  vec($rin,fileno($cmd),1) = 1;
  206.  
  207.  my $buf;
  208.  
  209.  until(scalar(@{${*$cmd}{'net_cmd_lines'}}))
  210.   {
  211.    my $timeout = $cmd->timeout || undef;
  212.    my $rout;
  213.    if (select($rout=$rin, undef, undef, $timeout))
  214.     {
  215.      unless (sysread($cmd, $buf="", 1024))
  216.       {
  217.        carp ref($cmd) . ": Unexpected EOF on command channel"
  218.         if $cmd->debug;
  219.        $cmd->close;
  220.        return undef;
  221.       } 
  222.  
  223.      substr($buf,0,0) = $partial;    ## prepend from last sysread
  224.  
  225.      my @buf = split(/\015?\012/, $buf);    ## break into lines
  226.  
  227.      $partial = length($buf) == 0 || substr($buf, -1, 1) eq "\012"
  228.         ? ''
  229.           : pop(@buf);
  230.  
  231.      map { $_ .= "\n" } @buf;
  232.  
  233.      push(@{${*$cmd}{'net_cmd_lines'}},@buf);
  234.  
  235.     }
  236.    else
  237.     {
  238.      carp "$cmd: Timeout" if($cmd->debug);
  239.      return undef;
  240.     }
  241.   }
  242.  
  243.  ${*$cmd}{'net_cmd_partial'} = $partial;
  244.  
  245.  shift @{${*$cmd}{'net_cmd_lines'}};
  246. }
  247.  
  248. sub ungetline
  249. {
  250.  my($cmd,$str) = @_;
  251.  
  252.  ${*$cmd}{'net_cmd_lines'} ||= [];
  253.  unshift(@{${*$cmd}{'net_cmd_lines'}}, $str);
  254. }
  255.  
  256. sub parse_response
  257. {
  258.  return ()
  259.     unless $_[1] =~ s/^(\d\d\d)(.?)//o;
  260.  ($1, $2 eq "-");
  261. }
  262.  
  263. sub response
  264. {
  265.  my $cmd = shift;
  266.  my($code,$more) = (undef) x 2;
  267.  
  268.  ${*$cmd}{'net_cmd_resp'} ||= [];
  269.  
  270.  while(1)
  271.   {
  272.    my $str = $cmd->getline();
  273.  
  274.    $cmd->debug_print(0,$str)
  275.      if ($cmd->debug);
  276.  
  277.    ($code,$more) = $cmd->parse_response($str);
  278.    unless(defined $code)
  279.     {
  280.      $cmd->ungetline($str);
  281.      last;
  282.     }
  283.  
  284.    ${*$cmd}{'net_cmd_code'} = $code;
  285.  
  286.    push(@{${*$cmd}{'net_cmd_resp'}},$str);
  287.  
  288.    last unless($more);
  289.   } 
  290.  
  291.  substr($code,0,1);
  292. }
  293.  
  294. sub read_until_dot
  295. {
  296.  my $cmd = shift;
  297.  my $arr = [];
  298.  
  299.  while(1)
  300.   {
  301.    my $str = $cmd->getline();
  302.  
  303.    $cmd->debug_print(0,$str)
  304.      if ($cmd->debug & 4);
  305.  
  306.    last if($str =~ /^\.\r?\n/o);
  307.  
  308.    $str =~ s/^\.\././o;
  309.  
  310.    push(@$arr,$str);
  311.   }
  312.  
  313.  $arr;
  314. }
  315.  
  316. sub datasend
  317. {
  318.  my $cmd = shift;
  319.  my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
  320.  my $line = join("" ,@$arr);
  321.  
  322.  return 1
  323.     unless length($line);
  324.  
  325.  if($cmd->debug)
  326.   {
  327.    my $b = "$cmd>>> ";
  328.    print STDERR $b,join("\n$b",split(/\n/,$line)),"\n";
  329.   }
  330.  
  331.  $line =~ s/\n/\015\012/sgo;
  332.  
  333.  ${*$cmd}{'net_cmd_lastch'} ||= " ";
  334.  $line = ${*$cmd}{'net_cmd_lastch'} . $line;
  335.  
  336.  $line =~ s/(\012\.)/$1./sog;
  337.  
  338.  ${*$cmd}{'net_cmd_lastch'} = substr($line,-1,1);
  339.  
  340.  my $len = length($line) - 1;
  341.  
  342.  return $len == 0 ||
  343.     syswrite($cmd, $line, $len, 1) == $len;
  344. }
  345.  
  346. sub dataend
  347. {
  348.  my $cmd = shift;
  349.  
  350.  return 1
  351.     unless(exists ${*$cmd}{'net_cmd_lastch'});
  352.  
  353.  if(${*$cmd}{'net_cmd_lastch'} eq "\015")
  354.   {
  355.    syswrite($cmd,"\012",1);
  356.    print STDERR "\n"
  357.     if($cmd->debug);
  358.   }
  359.  elsif(${*$cmd}{'net_cmd_lastch'} ne "\012")
  360.   {
  361.    syswrite($cmd,"\015\012",2);
  362.    print STDERR "\n"
  363.     if($cmd->debug);
  364.   }
  365.  
  366.  print STDERR "$cmd>>> .\n"
  367.     if($cmd->debug);
  368.  
  369.  syswrite($cmd,".\015\012",3);
  370.  
  371.  delete ${*$cmd}{'net_cmd_lastch'};
  372.  
  373.  $cmd->response() == CMD_OK;
  374. }
  375.  
  376. 1;
  377.  
  378. __END__
  379.  
  380.  
  381. =head1 NAME
  382.  
  383. Net::Cmd - Network Command class (as used by FTP, SMTP etc)
  384.  
  385. =head1 SYNOPSIS
  386.  
  387.     use Net::Cmd;
  388.     
  389.     @ISA = qw(Net::Cmd);
  390.  
  391. =head1 DESCRIPTION
  392.  
  393. C<Net::Cmd> is a collection of methods that can be inherited by a sub class
  394. of C<IO::Handle>. These methods implement the functionality required for a
  395. command based protocol, for example FTP and SMTP.
  396.  
  397. =head1 USER METHODS
  398.  
  399. These methods provide a user interface to the C<Net::Cmd> object.
  400.  
  401. =over 4
  402.  
  403. =item debug ( VALUE )
  404.  
  405. Set the level of debug information for this object. If C<VALUE> is not given
  406. then the current state is returned. Otherwise the state is changed to 
  407. C<VALUE> and the previous state returned. 
  408.  
  409. Set the level of debug information for this object. If no argument is
  410. given then the current state is returned. Otherwise the state is
  411. changed to C<$value>and the previous state returned.  Different packages
  412. may implement different levels of debug but, a  non-zero value result in
  413. copies of all commands and responses also being sent to STDERR.
  414.  
  415. If C<VALUE> is C<undef> then the debug level will be set to the default
  416. debug level for the class.
  417.  
  418. This method can also be called as a I<static> method to set/get the default
  419. debug level for a given class.
  420.  
  421. =item message ()
  422.  
  423. Returns the text message returned from the last command
  424.  
  425. =item code ()
  426.  
  427. Returns the 3-digit code from the last command. If a command is pending
  428. then the value 0 is returned
  429.  
  430. =item ok ()
  431.  
  432. Returns non-zero if the last code value was greater than zero and
  433. less than 400. This holds true for most command servers. Servers
  434. where this does not hold may override this method.
  435.  
  436. =item status ()
  437.  
  438. Returns the most significant digit of the current status code. If a command
  439. is pending then C<CMD_PENDING> is returned.
  440.  
  441. =item datasend ( DATA )
  442.  
  443. Send data to the remote server, converting LF to CRLF. Any line starting
  444. with a '.' will be prefixed with another '.'.
  445. C<DATA> may be an array or a reference to an array.
  446.  
  447. =item dataend ()
  448.  
  449. End the sending of data to the remote server. This is done by ensuring that
  450. the data already sent ends with CRLF then sending '.CRLF' to end the
  451. transmission. Once this data has been sent C<dataend> calls C<response> and
  452. returns true if C<response> returns CMD_OK.
  453.  
  454. =back
  455.  
  456. =head1 CLASS METHODS
  457.  
  458. These methods are not intended to be called by the user, but used or 
  459. over-ridden by a sub-class of C<Net::Cmd>
  460.  
  461. =over 4
  462.  
  463. =item debug_print ( DIR, TEXT )
  464.  
  465. Print debugging information. C<DIR> denotes the direction I<true> being
  466. data being sent to the server. Calls C<debug_text> before printing to
  467. STDERR.
  468.  
  469. =item debug_text ( TEXT )
  470.  
  471. This method is called to print debugging information. TEXT is
  472. the text being sent. The method should return the text to be printed
  473.  
  474. This is primarily meant for the use of modules such as FTP where passwords
  475. are sent, but we do not want to display them in the debugging information.
  476.  
  477. =item command ( CMD [, ARGS, ... ])
  478.  
  479. Send a command to the command server. All arguments a first joined with
  480. a space character and CRLF is appended, this string is then sent to the
  481. command server.
  482.  
  483. Returns undef upon failure
  484.  
  485. =item unsupported ()
  486.  
  487. Sets the status code to 580 and the response text to 'Unsupported command'.
  488. Returns zero.
  489.  
  490. =item response ()
  491.  
  492. Obtain a response from the server. Upon success the most significant digit
  493. of the status code is returned. Upon failure, timeout etc., I<undef> is
  494. returned.
  495.  
  496. =item parse_response ( TEXT )
  497.  
  498. This method is called by C<response> as a method with one argument. It should
  499. return an array of 2 values, the 3-digit status code and a flag which is true
  500. when this is part of a multi-line response and this line is not the list.
  501.  
  502. =item getline ()
  503.  
  504. Retrieve one line, delimited by CRLF, from the remote server. Returns I<undef>
  505. upon failure.
  506.  
  507. B<NOTE>: If you do use this method for any reason, please remember to add
  508. some C<debug_print> calls into your method.
  509.  
  510. =item ungetline ( TEXT )
  511.  
  512. Unget a line of text from the server.
  513.  
  514. =item read_until_dot ()
  515.  
  516. Read data from the remote server until a line consisting of a single '.'.
  517. Any lines starting with '..' will have one of the '.'s removed.
  518.  
  519. Returns a reference to a list containing the lines, or I<undef> upon failure.
  520.  
  521. =back
  522.  
  523. =head1 EXPORTS
  524.  
  525. C<Net::Cmd> exports six subroutines, five of these, C<CMD_INFO>, C<CMD_OK>,
  526. C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR> ,correspond to possible results
  527. of C<response> and C<status>. The sixth is C<CMD_PENDING>.
  528.  
  529. =head1 AUTHOR
  530.  
  531. Graham Barr <gbarr@ti.com>
  532.  
  533. =head1 COPYRIGHT
  534.  
  535. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  536. This program is free software; you can redistribute it and/or modify
  537. it under the same terms as Perl itself.
  538.  
  539. =cut
  540.